package com.diytomcat.catalina;

import java.util.List;

import com.diytomcat.util.ServerXMLUtil;

/**
 * @author Leon Plious
 * @decription tomcat Engine表示servlet引擎，用来处理servlet请求
 */
public class Engine {
	// 默认的host名称
	private String defaultHost;
	private List<Host> hosts;
	private Service service;
	
	public Engine(Service service) {
		// 通过 ServerXMLUtil 进行初始化
		this.defaultHost = ServerXMLUtil.getEngineDefaultHost();
		this.hosts = ServerXMLUtil.getHosts(this);
		this.service = service;
	}
	
	
	public Service getService() {
		return service;
	}

	public List<Host> getHosts() {
		return hosts;
	}

	public void checkDefault() {
		if(null == getDefaultHost())
			throw new RuntimeException("the defaultHost" + defaultHost + " does not exist!");
	}


	public Host getDefaultHost() {
		for(Host host : hosts) {
			if(host.getName().equals(defaultHost))
				return host;
		}
		return null;
	}
	
}
